package com.ruoyi.askcase.controller;

import java.util.List;
import java.util.Random;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.askcase.domain.AskCase;
import com.ruoyi.askcase.service.IAskCaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 病例管理Controller
 * 
 * @author ruoyi
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/ask/case")
public class AskCaseController extends BaseController
{
    @Autowired
    private IAskCaseService askCaseService;

    /**
     * 查询病例管理列表
     */
    @PreAuthorize("@ss.hasPermi('ask:case:list')")
    @GetMapping("/list")
    public TableDataInfo list(AskCase askCase)
    {
        startPage();
        List<AskCase> list = askCaseService.selectAskCaseList(askCase);
        return getDataTable(list);
    }

    /**
     * 导出病例管理列表
     */
    @PreAuthorize("@ss.hasPermi('ask:case:export')")
    @Log(title = "病例管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AskCase askCase)
    {
        List<AskCase> list = askCaseService.selectAskCaseList(askCase);
        ExcelUtil<AskCase> util = new ExcelUtil<AskCase>(AskCase.class);
        util.exportExcel(response, list, "病例管理数据");
    }

    /**
     * 获取病例管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ask:case:query')")
    @GetMapping(value = "/{caseId}")
    public AjaxResult getInfo(@PathVariable("caseId") Long caseId)
    {
        return success(askCaseService.selectAskCaseByCaseId(caseId));
    }

    /**
     * 新增病例管理
     */
    @PreAuthorize("@ss.hasPermi('ask:case:add')")
    @Log(title = "病例管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AskCase askCase)
    {
        //这个从MP 控制器搬过来的。正常不走这个接口 添加病例
        Random random = new Random();
        // 生成100000到999999之间的随机数，确保是6位数
        int randomNumber = 100000 + random.nextInt(900000);
        String code6 = ""+randomNumber;
        //String uuid = UUID.randomUUID().toString();
        String uuid = UUID.randomUUID().toString().replace("-", "").substring(0, 20);

        askCase.setCaseCode6(code6);
        askCase.setCaseUuid(uuid);
        return toAjax(askCaseService.insertAskCase(askCase));
    }

    /**
     * 修改病例管理
     */
    @PreAuthorize("@ss.hasPermi('ask:case:edit')")
    @Log(title = "病例管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AskCase askCase)
    {
        return toAjax(askCaseService.updateAskCase(askCase));
    }

    /**
     * 删除病例管理
     */
    @PreAuthorize("@ss.hasPermi('ask:case:remove')")
    @Log(title = "病例管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{caseIds}")
    public AjaxResult remove(@PathVariable Long[] caseIds)
    {
        return toAjax(askCaseService.deleteAskCaseByCaseIds(caseIds));
    }
}
